window.onload = function () {
    let c_height = document.documentElement.clientHeight;
    let c_width = document.documentElement.clientWidth;
    let need_left = (c_width - 900) / 2;
    let dia = document.getElementsByClassName('dialog_form');
    $(".dialog_form").css('left', need_left);
    $(".dialog_form").css('height', c_height);
    uploadify();
    let dept_name = $("#SuggestDept").val();
    $.ajax({
        url:"../ajax/find_name_array.php",
        type: "POST",
        data:{dept_name: dept_name},
        success: function (result) {
            name_array = result.split(",");
        }
    });
    c_height -= 40;
    $(".dailog_content_border").css("height", c_height);

}

function close_dialog() {
    $(".dialog_background",parent.document).remove();
    $(".dialog_ifr",parent.document).remove();
}

let files_num = 0;
function uploadify() {
    let sn = $("#SN").val();
    let state = $('#State').val();
    $("#uploadify").uploadifive({
        'auto': false,
        'method': 'post',
        'buttonText': "添加文件(必须上传图片文件且小于5M)",
        'height': 30,
        'width': 250,
        'uploadScript': 'uploadifive.php?sn=' + sn + '&state=' + state,
        'removeCompleted': false,
        'queueID': 'queue',
        'onSelect': function (file) {
            //$("#" + file.id).prepend('<div style="float:left;width:50px;margin-right:2px;"><img src="/Content/images/filetype/' + file.type.replace('.', '')  + '.png" style="width:40px;height:40px;" /></div>');
            //$('.border').hide();
        },
        'onAddQueueItem':function(file) {
            files_num++;
            console.log(files_num);
        },
        'onError': function (errorType, file) {
            if (file != 0) {
                $("#file_upload").uploadifive("debug"); //在控制台输出调试信息
                let settings = $('#file_upload').data('uploadifive').settings;
                switch (errorType) {
                    case 'UPLOAD_LIMIT_EXCEEDED':
                        alert("上传的文件数量已经超出系统限制的" + settings.queueSizeLimit + "个文件！");
                        break;
                    case 'FILE_SIZE_LIMIT_EXCEEDED':
                        alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#file_upload').uploadifive('settings', 'fileSizeLimit') + "大小！");
                        break;
                    case 'QUEUE_LIMIT_EXCEEDED':
                        alert("任务数量超出队列限制");
                        break;
                    case 'FORBIDDEN_FILE_TYPE':
                        alert("文件 [" + file.name + "] 类型不正确！");
                        break;
                    case '404_FILE_NOT_FOUND':
                        alert("文件未上传成功或服务器存放文件的文件夹不存在");
                        break;
                }
            }
        },
        'onCancel': function (file) {
            files_num--;
            console.log(files_num);
        },
        'onUploadComplete': function (file, data) {
            if (data) {
                console.log(data);
            }
        }
    });
}

let name_array = new Array();
function add_sug_complete(){
    let sn = $("#SN").val();
    let addtime = $("#AddTime").val();
    let account = $("#Account").val();
    let addname = $("#AddName").val();
    let addphone = $("#AddPhone").val();
    let state = $("#State").val();
    let adduserdept = $("#AddUserDept").val();
    let s_title = $("#S_Title").val();
    let s_type = $("#S_Type").val();
    let importantlevel = $("#Suggest_importantLevel").val();
    let currentdesc = $("#CurrentDesc").val();
    let improvemethod = $("#ImproveMethod").val();
    let anticipateresult = $("#AnticipateResult").val();
    let suggestdepartment = $("#SuggestDept").val();
    let suggestimpl = $("#SuggestImpl").val();
    if(!s_title || !s_type || !importantlevel || !currentdesc || !improvemethod || !anticipateresult){
        alert("合理化建议信息填写不完整！");
        return;
    }
    if(suggestimpl != null){
        let person_name = false;
        name_array.forEach((item,num,array)=>{
            if(suggestimpl == item){
                person_name = true;
            }
        })
        if(!person_name){
            alert("实施人姓名填写错误！");
            return;
        }
    }
    if(files_num <= 0){
        alert("请添加附件！");
        return;
    }
    $('#uploadify').uploadifive('upload');
    $.ajax({
        url: "../ajax/need_to_sql.php",
        type: "POST",
        data: {
            SN: sn,
            AddTime: addtime,
            AddUserId: account,
            AddUserName: addname,
            AddUserMobile: addphone,
            //State: state,
            AddUserDept: adduserdept,
            S_Title: s_title,
            S_Type: s_type,
            ImportantLevel: importantlevel,
            CurrentDesc: currentdesc,
            ImproveMethod: improvemethod,
            AnticipateResult: anticipateresult,
            SuggestDept: suggestdepartment,
            SuggestImpl: suggestimpl,
            Delete_flag: 0,
            add_sug_complete: 1
        },
        success: function (result) {
            alert(result);
            close_dialog();
        }
    });
}

function new_dept_person() {
    let dept_name = $("#SuggestDept").val();
    $.ajax({
        url:"../ajax/find_name_array.php",
        type: "POST",
        data:{dept_name: dept_name},
        success: function (result) {
            name_array = result.split(",");
        }
    });
}

function autocomplete_name() {
    if(name_array != null){
        $('#SuggestImpl').autocomplete({
            source: name_array
        });
    }
}

function is_suggest() {
    let result = $('#is_pass').is(':checked');
    if(result == false){
        $('#reason1').attr("disabled", false);
        $('#reason2').attr("disabled", false);
        $('#reason3').attr("disabled", false);
        $('#reason4').attr("disabled", false);
        $('#reason5').attr("disabled", false);
        $('#reason6').attr("disabled", false);
        $('#Auditor').attr("disabled", "disabled");
        $('#start_score').attr("disabled", "disabled");
        $('#Score').attr("disabled", "disabled");
    }
    else {
        $.ajax({
            url:"../ajax/find_name_array.php",
            type: "POST",
            data:{dept_name: "ALL"},
            success: function (result) {
                name_array = result.split(",");
                $('#Auditor').autocomplete({
                    source: name_array
                });
            }
        });
        $('#reason1').attr("disabled", "disabled");
        $('#reason2').attr("disabled", "disabled");
        $('#reason3').attr("disabled", "disabled");
        $('#reason4').attr("disabled", "disabled");
        $('#reason5').attr("disabled", "disabled");
        $('#reason6').attr("disabled", "disabled");
        $('#Auditor').attr("disabled", false);
        $('#start_score').attr("disabled", false);
        $('#Score').attr("disabled", false);
    }
}

function is_complete() {
    let result = $('#is_complete').is(':checked');
    if(result == false){
        $('#Auditor').attr("disabled", false);
        $('#RefuseReason').attr("disabled", false);
        $.ajax({
            url:"../ajax/find_name_array.php",
            type: "POST",
            data:{dept_name: "ALL"},
            success: function (result) {
                name_array = result.split(",");
                $('#Auditor').autocomplete({
                    source: name_array
                });
            }
        });
    }
    else {
        $('#Auditor').attr("disabled", "disabled");
        $('#RefuseReason').attr("disabled", "disabled");
    }
}

function ci_check_complete() {
    let sn = $("#SN").val();
    let Notice = $("#Notice").val();
    let result = $('#is_pass').is(':checked');
    if(result == false){
        let WrongReason = $("input[name='textbox']:checked").val();
        if(WrongReason == null && Notice == null){
            alert("请填写拒绝原因！");
            return;
        }
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                WrongReason: WrongReason,
                Notice: Notice,
                ci_check_complete: 1,
                is_suggest: 0
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {ci_key_words: "", ci: "1"},
                    success: function (result1) {
                        $(window.parent.$("#461eb79f-014f-4f13-b9ef-53f9146205e9")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }else {
        let auditor_str = $("#Auditor").val();
        let auditor_array = auditor_str.split('|');
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                AcceptUserId: auditor_array[1],
                Notice: Notice,
                ci_check_complete: 1,
                is_suggest: 1
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {ci_key_words: "", ci: "1"},
                    success: function (result1) {
                        $(window.parent.$("#461eb79f-014f-4f13-b9ef-53f9146205e9")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }
}

function show_score() {
    $.ajax({
        url: "../dialog/dialog_in_border.php",
        type: "POST",
        data: {show_score: 1},
        success: function (result) {
            $(".dailog_content_border").after(result);
            dialog_in_fit_screen();
        }
    });
}

function is_accept(){
    let is_accept = $('#is_accept').is(':checked');
    if(is_accept){
        $('#ForecastTime').attr("disabled", false);
        $('#ImplRefuseReason').attr("disabled", "disabled");
    }
    else{
        $('#ForecastTime').attr("disabled", "disabled");
        $('#ImplRefuseReason').attr("disabled", false);
    }
}

function accept_complete(){
    let sn = $("#SN").val();
    let is_accept = $('#is_accept').is(':checked');
    if(is_accept){
        let forecast_time = $('#ForecastTime').val();
        if(!forecast_time){
            alert("请填写承诺时间！");
            return;
        }
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                accept_complete: 1,
                is_accept: 1,
                forecast_time: forecast_time
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {my_impl_words: "", my_impl: "1"},
                    success: function (result1) {
                        $(window.parent.$("#100c7db7-e23f-4631-84ea-c79c313125d6")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }
    else {
        let ImplRefuseReason = $('#ImplRefuseReason').val();
        if(!ImplRefuseReason){
            alert("请填写拒绝原因！");
            return;
        }
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                ImplRefuseReason: ImplRefuseReason,
                accept_complete: 1,
                is_accept: 0
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {my_impl_words: "", my_impl: "1"},
                    success: function (result1) {
                        $(window.parent.$("#100c7db7-e23f-4631-84ea-c79c313125d6")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }
}

function do_complete(){
    let sn = $("#SN").val();
    let PracticalMeasuals = $('#PracticalMeasuals').val();
    if(!PracticalMeasuals){
        alert("必须填写实际措施！");
        return ;
    }
    if(files_num <= 0){
        alert("请添加附件！");
        return;
    }
    $('#uploadify').uploadifive('upload');
    $.ajax({
        url: "../ajax/need_to_sql.php",
        type: "POST",
        data: {
            SN: sn,
            PracticalMeasuals: PracticalMeasuals,
            do_complete: 1
        },
        success: function (result) {
            $.ajax({
                url: "../ajax/table.php",
                type: "POST",
                data: {my_impl_words: "", my_impl: "1"},
                success: function (result1) {
                    $(window.parent.$("#100c7db7-e23f-4631-84ea-c79c313125d6")).contents().find(".contentPanel").html(result1);
                    close_dialog();
                }
            });
        }
    });
}

function dialog_in_fit_screen() {
    let c_height = document.documentElement.clientHeight;
    c_height -= 41;
    $('.dialog_in_ifr').height(c_height);
}

function close_in_dialog() {
    $(".dialog_in_background",parent.document).remove();
    $(".dialog_in_ifr",parent.document).remove();
}

function score_complete() {
    let score_all = 0;
    $('input').each(function () {
        let is_accept = $(this).is(':checked');
        if(is_accept){
            score_all += parseInt($(this).val());
        }
    });
    console.log(score_all);
    $('#Score',parent.document).val(score_all);
    close_in_dialog();
}

function auditor_check_complete() {
    let sn = $("#SN").val();
    let result = $('#is_pass').is(':checked');
    if(!result){
        let WrongReason = $("#RefuseReason").val();
        if(!WrongReason){
            alert("请填写拒绝原因！");
            return;
        }
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                WrongReason: WrongReason,
                auditor_check_complete: 1,
                is_suggest: 0
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {auditor_key_words: "", auditor: "1"},
                    success: function (result1) {
                        $(window.parent.$("#dd6b7887-2276-47bc-9e16-eb62d1132a61")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }else {
        let auditor_str = $("#Auditor").val();
        let auditor_array = auditor_str.split('|');
        let score = $('#Score').val();
        if(!auditor_str){
            alert("请选择实施人！");
            return;
        }
        $.ajax({
            url: "../ajax/need_to_sql.php",
            type: "POST",
            data: {
                SN: sn,
                AcceptUserId: auditor_array[1],
                Score: score,
                auditor_check_complete: 1,
                is_suggest: 1
            },
            success: function () {
                $.ajax({
                    url: "../ajax/table.php",
                    type: "POST",
                    data: {auditor_key_words: "", auditor: "1"},
                    success: function (result1) {
                        $(window.parent.$("#dd6b7887-2276-47bc-9e16-eb62d1132a61")).contents().find(".contentPanel").html(result1);
                        close_dialog();
                    }
                });
            }
        });
    }
}